package com.ly.spring.mvc.web;


import com.ly.spring.mvc.entity.User;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.print.attribute.standard.MediaTray;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.management.MemoryType;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("test")
public class HelloAction {


    @GetMapping(value = "hello",produces = MediaType.TEXT_HTML_VALUE+";charset=utf-8")
    public String hello(String name) {
        System.out.println("hello"); return "hello " + name;}

    @RequestMapping(path = "demo", method = {RequestMethod.POST,RequestMethod.DELETE})
    public String demo(String name) {return "demo " + name;}

    @RequestMapping("get")
    public String get(String name) {return "get " + name;}

    @RequestMapping("post")
    public String post(String name) {return "post " + name;}

    @RequestMapping(path = "param", params = {"a","b=1","c!=2"})
    public String param(String name) {return "param " + name;}

    @RequestMapping(path = "header", headers = {"connection=keep-alive"})
    public String header(String name) {return "header " + name;}


    @GetMapping("add")
    public int add(int a, int b) {return a + b;}


    @GetMapping("servlet")
    public Map<String,Object> servlet(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session){
        Map<String,Object> ret = new HashMap<>();
        ret.put("request", request);
        ret.put("response", response);
        ret.put("session", session);
        return ret;
    }

    @GetMapping("user")
    public User user(User user){
        user.setAge(user.getAge()+10);
        return user;
    }

    @GetMapping("sub/{c}")
    public int sub(@RequestParam("a") int x,
                   @RequestParam(name="b",
                           required = false,
                           defaultValue = "10") int y,
                   @PathVariable(value = "c") int z){
        return x - y - z;
    }

        @GetMapping("sub")
        public Map<String, Integer> sub(@RequestParam("a") int x,
                 @RequestParam(name="b", required = false,
                         defaultValue = "10") int y) {
            Map<String, Integer> result = new HashMap<>();
            result.put("result", x - y);
            return result;
        }

    @GetMapping(value = "headers")
    public String headers(
            @RequestHeader String Accept,
            @RequestHeader("Host") String host,
            @RequestHeader("Cookie") String cookie,
            @CookieValue String name,
            @CookieValue("myAge") String age

    ){
        return String.format("<h1>%s</h1>" +
                        "<h1>%s</h1>" +
                        "<h1>%s</h1>" +
                        "<h1>%s</h1>" +
                        "<h1>%s</h1>",
                Accept,
                host,
                cookie,name,age);
    }

    @GetMapping("toTest")
    public ModelAndView toTest(ModelAndView mav){

        mav.addObject("myname","李四");
        mav.setViewName("/test.jsp");

        return mav;

    }

    @GetMapping("toPay")
    ModelAndView toPay(ModelAndView mav){
        mav.setViewName("redirect:/spring_war_exploded/pay.html");
        return mav;
    }


}
